package com.yfp.operation.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.domain.AiPrompt;
import com.yfp.operation.mapper.AiPromptMapper;
import com.yfp.operation.service.AiPromptService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 *
 * @description 针对表【ai_prompt(AI模型提示词内容表)】的数据库操作Service实现
 * @createDate 2025-02-24 15:08:52
 */
@Service
public class AiPromptServiceImpl extends ServiceImpl<AiPromptMapper, AiPrompt> implements AiPromptService {

    @Resource
    private AiPromptMapper aiPromptMapper;

    @Override
    public AiPrompt searchAiPrompt() {
        LambdaQueryWrapper<AiPrompt> query = Wrappers.lambdaQuery(AiPrompt.class);
        query.orderByDesc(AiPrompt::getId);
        query.last("limit 1");

        AiPrompt aiPrompt = aiPromptMapper.selectOne(query);
        return aiPrompt;
    }

    @Override
    public void updateAiPrompt(AiPrompt aiPrompt) {
        DateTime date = DateUtil.date();
        Long userId = SecurityUtils.getUserId();

        aiPrompt.setUpdateTime(date);
        aiPrompt.setUpdateUserId(userId);
        aiPromptMapper.updateById(aiPrompt);
    }

    @Override
    public void saveAiPrompt(AiPrompt aiPrompt) {
        DateTime date = DateUtil.date();
        Long userId = SecurityUtils.getUserId();

        aiPrompt.setCreateTime(date);
        aiPrompt.setCreateUserId(userId);
        aiPrompt.setUpdateTime(date);
        aiPrompt.setUpdateUserId(userId);
        aiPromptMapper.insert(aiPrompt);
    }
}




